package HomeWork.Question3;

class Thread_Sync {
    public static void main(String[] args) {
        Costomer c1 = new Costomer();
        new Thread(c1, "张三").start();
        new Thread(c1, "赵六").start();
        new Thread(c1, "王五").start();
        new Thread(c1, "李四").start();
    }
}

class Costomer implements Runnable {
    private String ticket_name;
    @Override
    public void run() {                         //重写run方法   
       while ((ticket_name = Ticket.getTicket()) != "NULL") {     
           System.out.println(Thread.currentThread().getName() + " 买走座位号为" + ticket_name + "的票");
       }
       System.out.println("票卖完了！");
       return;
    }
    
}

class Ticket{
    private static char letter = 65;
    private static short num = 1;
    public static synchronized String getTicket(){          //互斥锁
        try {
                Thread.sleep(1000);                            // 取票时间1s
            } catch (InterruptedException e) {          
                e.printStackTrace();
            }
        if(num > 10 ){
            num = 1;
            letter++;
        }
        if(letter > 70)                     // 没有票了
            return "NULL";
        return ""+letter+num++;
    }
}